import { AppDispatch } from '..';
import { getsearchlistAPI, getSuggestsAPI } from '@/api/search';
import { SearchActionType } from '../reducer/search';

/** 获取搜索建议 */
export const getSuggestsAction = (keyword: string) => {
  return async (dispatch: AppDispatch) => {
    const res = await getSuggestsAPI(keyword);
    dispatch({ type: 'search/saveList', payload: res.data.options });
  };
};

/** 清空输入框 */
export const clearSuggestsAction = () => {
  return {
    type: 'search/clearList',
  };
};
/** 历史记录 */
export const saveHistoriesAction = (payload: string): SearchActionType => {
  return {
    type: 'search/saveHistories',
    payload,
  };
};
/** 清除历史记录 */
export const clearHistoriesAction = () => {
  return {
    type: 'search/clearHistories',
  };
};
// 获取搜索结果
export const getresultAction = (keyword: string, page = 1, per_page = 10) => {
  return async (dispatch: AppDispatch) => {
    const res = await getsearchlistAPI({ keyword, page, per_page });
    dispatch({ type: 'search/saveResults', payload: res.data.results });
    console.log('res', res);
  };
};
// 清除数据
export const clearresultAction = () => {
  return {
    type: 'search/clearResults',
  };
};
